package com.example.baselibrary.db.dao;

import com.example.baselibrary.db.BTDBHelper;
import com.example.baselibrary.db.table.FileDownLoadTable;

import java.sql.SQLException;
import java.util.Locale;
import cc.vv.lklibrary.db.dao.DBBaseTableDao;
import cc.vv.lklibrary.db.help.DBBaseOpenHelper;
import cc.vv.lklibrary.log.LogOperate;

/**
 * 文件下载dao
 *
 * @author Chen.qi
 */
public class FileDownLoadDao extends DBBaseTableDao<FileDownLoadTable, String> {

    public FileDownLoadDao(DBBaseOpenHelper openHelper) {
        super(openHelper);
    }

    private static FileDownLoadDao mInstance;

    public static FileDownLoadDao getInstance() {
        if (null == mInstance) {
            synchronized (FileDownLoadDao.class) {
                if (null == mInstance) {
                    mInstance = new FileDownLoadDao(BTDBHelper.getInstance());
                }
            }
        }
        return mInstance;
    }

    /**
     * 插入文件数据
     *
     * @param fileDownLoadTable
     * @return
     */
    public int insertOrUpdateFileDownload(FileDownLoadTable fileDownLoadTable) {
        int result = -1;
        if (null == fileDownLoadTable) {
            return result;
        }
        try {
            FileDownLoadTable queryTableBean = mDao.queryForId(fileDownLoadTable.saveFilePath);
            if (null == queryTableBean) {
                result = mDao.create(fileDownLoadTable);
            } else {
                mDao.update(fileDownLoadTable);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            LogOperate.e(String.format(Locale.CHINESE, "数据添加失败,原因：%s", e.getMessage()));
        }
        return result;
    }


}
